package com.mijie.homi.admin.task;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.mijie.core.ApplicationProperties;
import com.mijie.homi.admin.dao.reports.ReportDao;
import com.mijie.homi.admin.domain.commom.CrashStat;


public class CrashStatTask {
	
	private Logger logger = Logger.getLogger(CrashStatTask.class);
	
	@Autowired
	private ReportDao dao;
	
	public void work(){
		List<CrashStat> list = dao.getCrashList4Stat();
		if(list == null || list.isEmpty()){
			logger.info("crashStatInfo:there is no crash info in that day.");
			return;
		}
		Map<String, CrashStat> map = new HashMap<String, CrashStat>();
		for(CrashStat bean : list){
			String version = bean.getVersion();
			version = version.substring(version.indexOf("|") + 1);
			if(map.containsKey(version)){
				CrashStat temp = map.get(version);
				temp.setBugs(temp.getBugs() + bean.getBugs());
				temp.setCrashes(temp.getCrashes() + bean.getCrashes());
			}else{
				map.put(version, bean);
			}
		}
		
		String logPath = ApplicationProperties.get("homiServerLogPath");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		logPath = logPath + "." + yesterday;
		
		try {
			String encoding="UTF-8";
	        File file=new File(logPath);
	        if(file.isFile() && file.exists()){ 
	            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
	            BufferedReader bufferedReader = new BufferedReader(read);
	            String lineTxt = null;
	            Pattern p = Pattern.compile("@startApp@:appversion:([^,]+),", Pattern.CASE_INSENSITIVE);
				while((lineTxt = bufferedReader.readLine()) != null){
				    Matcher m = p.matcher(lineTxt);
				    while(m.find()){
				     	String ver = m.group(1);
				     	if(map.containsKey(ver)){
				     		CrashStat stat = map.get(ver);
				     		stat.setStarts(stat.getStarts() + 1);
				     	}
				    }
				}
				read.close();
				for(Map.Entry<String, CrashStat> entry : map.entrySet()){
					CrashStat bean = entry.getValue();
					bean.setVersion(entry.getKey()); 
					logger.info("@crashStat@--version:" + bean.getVersion() + ",crashes:" + bean.getCrashes() + ",bugs:" + bean.getBugs()
							+ ",starts:" + bean.getStarts());
					dao.insertCrashStat(bean);
				}
			}
		}catch (IOException e) {
				logger.error(e);
				e.printStackTrace();
		}
    }
		
	
	public static void main(String[] args) {
		String str = "@startApp@:appversion:1.0.5,";
		Pattern p = Pattern.compile("@startApp@:appversion:([^,]+),", Pattern.CASE_INSENSITIVE);
		 Matcher m = p.matcher(str);
		    while(m.find()){
		     	System.out.println(m.group(1));
		    }
	}
	
	
}
